// pages/street/list/list.js
var streetservice = require("../../../apiservice/street/streetservice");
var streetId = ''; //记录当前streetId
var currentIndex = -1; //记录当前选定的街道的坐标
var type = ''; //用于判断是进入详细页面还是进入其他页面
var searchKey = ''; //搜索参数
const userInfo = getApp().globalData.userInfo;
Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		customItem: "全部",
		editShow: false, //控制编辑窗口的显示
		region: ['湖南省', '长沙市', '天心区'],
		streetList: [],
		currentValue: '', //记录当前编辑的文本框
		userid: '',
		showDistrict: false
	},

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function(options) {
		if (userInfo != null) {
			this.setData({
				userid: userInfo.userid
			})
			var region = [];
			region.push(userInfo.new_provincename);
			region.push(userInfo.new_cityname);
			if (userInfo.new_areaname) {
				region.push(userInfo.new_areaname);
			} else {
				region.push('');
			}
			this.setData({
				region: region
			})
		}

		if (options.province && options.city && options.area) {
			this.setData({
				region: [options.province, options.city, options.area]
			})
		}

		//设置进入的类型
		type = options.type;
	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: function() {

		//加载默认地区的街道信息
		this.getStreetByPCA();

	},
	/**
	 * 当地区切换时，获取对应地区的街道信息
	 * @param {*} e 
	 */
	districtTap: function(e) {
	
		this.setData({
			showDistrict: true
		});
		
	},
	
	
		

	// areaConfirm: function (e) {
	//   console.log(e.detail);
	//   this.setData({ showDistrict: false });
	//   var provinceName = e.detail.currentTarget.dataset.province;
	//   var cityName = e.detail.currentTarget.dataset.city;
	//   var areaName = e.detail.currentTarget.dataset.area;
	//   //if(e.detail.values.length>0){

	//   wx.showLoading({ //期间为了显示效果可以添加一个过度的弹出框提示“加载中”  
	//     title: '加载中',
	//     icon: 'loading'
	//   });
	//   //console.log('picker发送选择改变，携带值为', e.detail.value);
	//   if (provinceName == "全部") {
	//     wx.showToast({
	//       title: "省区不能为全部",
	//       icon: "none",
	//       mask: true,
	//       duration: 2500
	//     })
	//     return;
	//   }
	//   this.getCommunityByPCA(provinceName, cityName, areaName);
	//   var region = [];
	//   region.push(provinceName);
	//   region.push(cityName);
	//   region.push(areaName);
	//   this.setData({
	//     region: region,
	//     street: [{
	//       new_name: '全部',
	//       new_streetid: null
	//     }],
	//     streetIndex: 0
	//   });

	//   this.GetStreetByPCA(this.data.region[0], this.data.region[1], this.data.region[2]);
	//   wx.hideLoading();
	//   //}
	// },
	bindRegionChange: function(e) {
		console.log('picker发送选择改变，携带值为', e.detail.currentTarget.dataset);
		this.setData({
			showDistrict: false
		});
		var provinceName = e.detail.currentTarget.dataset.province;
		var cityName = e.detail.currentTarget.dataset.city;
		var areaName = e.detail.currentTarget.dataset.area;
		var region = [];
		region.push(provinceName);
		region.push(cityName);
		region.push(areaName);
		if (provinceName != '全部') {
			this.setData({
				region: region
			})
			if (searchKey != '') {
				this.getStreetsByName();
			} else {
				this.getStreetByPCA();
			}
		} else {
			wx.showToast({
				title: "省区不能选全部",
				mask: true,
				icon: 'none',
				duration: 2000
			});
			return;
		}

		// let region = e.detail.value;
		// if (region[0] !== '全部') {
		// 	this.setData({
		// 		region: region
		// 	})
		// 	if (searchKey != '') {
		// 		this.getStreetsByName();
		// 	} else {
		// 		this.getStreetByPCA();
		// 	}
		// } else {
		// 	wx.showToast({
		// 		title: "省区不能选全部",
		// 		mask: true,
		// 		icon: 'none',
		// 		duration: 2000
		// 	});
		// 	return;
		// }
	},

	editItem: function(e) {
		// console.log(e);
		streetId = e.currentTarget.dataset.id;
		currentIndex = e.currentTarget.dataset.index;


		this.setData({
			editShow: true,
			currentValue: e.currentTarget.dataset.name ? e.currentTarget.dataset.name : ''
		});



	},
	/**
	 * 监听街道名修改
	 */
	onChange: function(e) {
		//console.log(e);
		this.setData({
			currentValue: e.detail
		})
	},
	/**
	 * 修改街道信息
	 * 
	 */
	onSure: function(e) {

		var that = this;
		var newName = this.data.currentValue; //街道名
		if (newName == '' || newName == null) {
			wx.showModal({
				title: '错误',
				showCancel: false,
				content: '街道名不能为空'

			});
			return false;
		}
		var street = 'streetList[' + currentIndex + '].new_name' //当前被选中的街道的名字属性
		//console.log(e);
		var streetobj = {
			new_streetId: streetId,
			new_name: newName
		};
		// console.log(streetobj)
		// console.log(street);
		// console.log(newName);
		//修改街道信息
		streetservice.modStreetInfo(streetobj).then(function(success) {

				//console.log(success);
				that.setData({
					editShow: false,
					[street]: newName
				});

			})
			.catch(function(ex) {
				//debugger;
				wx.showModal({
					title: '错误',
					showCancel: false,
					content: '更新失败'
				});
				that.setData({
					editShow: false
				});
				console.log(ex);
			})


	},


	onClose: function() {
		this.setData({
			editShow: false
		})
	},
	/**
	 * 跳转到街道详细信息页面
	 * @param {*} e 
	 */
	enterDetail: function(e) {
		streetId = e.currentTarget.id;

		if (type == 'detail') {
			wx.navigateTo({
				url: "/pages/street/detail/detail?detailId=" + streetId
			})
		} else if (type == 'choose') {
			debugger;
			let streetItem = e.currentTarget.dataset.item;
			//  wx.navigateBack({
			//    url: "../../community/add/add?Id=" + streetId + "&name=" + e.currentTarget.dataset.name
			//  })
			let pages = getCurrentPages();
			let currPage = pages[pages.length - 1]; //当前页面
			let prevPage = pages[pages.length - 2]; //上一个页面

			let province = this.data.region[0];
			let city = this.data.region[1];
			let area = this.data.region[2];
			if (streetItem.new_provinceName &&
				streetItem.new_cityName &&
				streetItem.new_areaName) {
				province = streetItem.new_provinceName;
				city = streetItem.new_cityName;
				area = streetItem.new_areaName;
			}

			//直接调用上一个页面对象的setData()方法，把数据存到上一个页面中去
			prevPage.setData({
				street: {
					new_streetid: streetId,
					new_name: e.currentTarget.dataset.name,
					province: province,
					city: city,
					area: area
				},
				position: {
					provinceid: province,
					// provincename:province,
					cityid: city,
					// cityname: city,
					countyid: area,
					// areaname: area
				}
			});
			//debugger
			wx.navigateBack({
				delta: 1
			})

		}

	},
	/**
	 * 根据街道名称获取街道列表
	 */
	onSearch: function(e) {
		//debugger;
		console.log(e);

		searchKey = e.detail.searchKey; //记录搜索参数
		if (searchKey) {
			this.getStreetsByName();
		} else {
			this.getStreetByPCA();
		}

	},
	/**
	 * 跳转到添加页面
	 */
	toAddPage: function() {
		let province = this.data.region[0];
		let city = this.data.region[1];
		let area = this.data.region[2];
		if (city === '全部' || area === '全部') {
			province = userInfo.new_provincename;
			city = userInfo.new_cityname;
			if (userInfo.new_areaname) {
				area = userInfo.new_areaname;
			}
		}
		wx.navigateTo({
			url: "../add/add?province=" +
				province +
				"&city=" + city +
				"&area=" + area
		});
	},



	/**
	 * 获取街道信息列表
	 * @param {*} province 
	 * @param {*} city 
	 * @param {*} area 
	 */
	getStreetByPCA: function() {
		var that = this;
		streetservice.getStreetByPAC(this.data.region[0], this.data.region[1], this.data.region[2]).then(function(
			success) {
			console.log(success);
			that.setData({
				streetList: success.data
			});
		}).catch(function(ex) {
			console.log(ex);
		})
	},
	getStreetsByName: function() {
		//console.log(searchKey);
		var that = this;
		streetservice.getStreetByName(searchKey, this.data.region[0], this.data.region[1], this.data.region[2]).then(
			function(success) {
				console.log(success);
				that.setData({
					streetList: success.data
				});
			}).catch(function(ex) {
			console.log(ex);
		})
	}
})
